package com.tekj.cxqcb.view.Module.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tekj.cxqcb.MainBActivity;
import com.tekj.cxqcb.R;
import com.tekj.cxqcb.operation.BusinessDao;
import com.tekj.cxqcb.view.Module.fragment.BuyingIntentionFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import common.base.activity.BaseActivity;
import common.base.adapter.TheFragmentPagerAdapter;

/**
 * 购车咨询
 */
public class CarConsultationActivity extends BaseActivity {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_l)
    TextView tvL;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_r)
    ImageView ivR;
    @BindView(R.id.tv_r)
    TextView tvR;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.rl_title_bg)
    RelativeLayout rlTitleBg;
    @BindView(R.id.tv_tab1)
    TextView tvTab1;
    @BindView(R.id.tv_tab2)
    TextView tvTab2;
    @BindView(R.id.vp_main)
    ViewPager vpMain;
    private List<Fragment> fragments;
    private BusinessDao businessDao;

    @Override
    public void init() {
        tvTitle.setText("购车咨询");
        fragments = new ArrayList<>();
        businessDao = new BusinessDao(activity, this);
        for (int i = 0; i < 2; i++) {
            newFragemt(i);
        }

        TheFragmentPagerAdapter adapter = new TheFragmentPagerAdapter(activity.getSupportFragmentManager(), fragments);
        vpMain.setAdapter(adapter);

        vpMain.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {

                setTextViewBg(i, true);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

    }

    @Override
    public int setLayout() {
        return R.layout.activity_car_consultation;
    }

    @Override
    public void initLoadStart() {

    }

    @Override
    public void initLoadEnd() {

    }

    @Override
    public void initBindingView(Object mBean) {

    }


    @OnClick({R.id.ll_back, R.id.tv_tab1, R.id.tv_tab2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
            case R.id.tv_tab1:
                setTextViewBg(0, false);
                break;
            case R.id.tv_tab2:
                setTextViewBg(1, false);
                break;
        }
    }

    void setTextViewBg(int type, boolean isvViewP) {
        if (type == 0) {
            tvTab1.setBackgroundResource(R.drawable.bg_l5_b);
            tvTab2.setBackgroundResource(R.drawable.bg_r5_b_kong);
            tvTab1.setTextColor(getResources().getColor(R.color.white));
            tvTab2.setTextColor(getResources().getColor(R.color.colorPrimary));
        } else {
            tvTab1.setBackgroundResource(R.drawable.bg_l5_b_kong);
            tvTab2.setBackgroundResource(R.drawable.bg_r5_b);
            tvTab1.setTextColor(getResources().getColor(R.color.colorPrimary));
            tvTab2.setTextColor(getResources().getColor(R.color.white));
        }
        if (!isvViewP) {
            vpMain.setCurrentItem(type);
        }
    }

    private void newFragemt(int type) {
        BuyingIntentionFragment fragment = new BuyingIntentionFragment();
        Bundle b = new Bundle();
        b.putInt("type", type);
        fragment.setArguments(b);
        fragments.add(fragment);
    }

    @Override
    protected void onStop() {
        super.onStop();
        businessDao.UpdateReadByUser(MainBActivity.User.getUserId());
    }
}
